1056C - Pick Heroes - CodeForces Solution


greedy implementation interactive sortings *1700

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <set>
using namespace std;

namespace solve
{
    const int maxn = 1e5 + 10;
    typedef long long ll;

    set<pair<int, int>, greater<pair<int, int>>> S, T;
    int bel[maxn], del[maxn];
    int a[maxn], m, n;
    int u[maxn], v[maxn];
    int qwq[maxn];

    void main()
    {
        cin >> n >> m;
        for (int i = 1; i <= n * 2; i++)
            cin >> a[i];
        for (int i = 1; i <= m; i++)
        {
            int x, y;
            cin >> x >> y, bel[x] = i, bel[y] = i;
            u[i] = x, v[i] = y;
        }
        int type, x = -1;
        cin >> type;
        type %= 2;
        for (int t = 0; t < n * 2; t++, type ^= 1)
        {
            if (type == 0)
            {
                // cerr << "??????????" << x << endl;
                cin >> x;
                del[x] = 1;
            }
            else
            {
                if (x != -1 && bel[x] && !qwq[bel[x]])
                {
                    int p = u[bel[x]], q = v[bel[x]];
                    int out = x == p ? q : p;
                    qwq[bel[x]] = 1;
                    cout << out << endl;
                    del[out] = 1;
                    del[x] = 1;
                }
                else
                {
                    del[x] = 1;
                    int flag = 0;
                    for (int i = 1; i <= m; i++)
                        if (!del[u[i]] && !del[v[i]])
                        {
                            int x = u[i], y = v[i];
                            if (a[y] > a[x])
                                swap(x, y);
                            cout << x << endl;
                            del[x] = 1;
                            flag = 1;
                            qwq[i] = 1;
                            break;
                        }
                    if (flag == 0)
                    {
                        S.clear();
                        for (int i = 1; i <= n * 2; i++)
                            if (!del[i])
                                S.insert({a[i], i});
                        int x = S.begin()->second;
                        del[x] = 1;
                        cout << x << endl;
                    }
                }
            }
        }
    }
}

int main()
{
    ios::sync_with_stdio(false), cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--)
        solve::main();
}


Comments

Submit
0 Comments
More Questions

507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm